const express = require('express')
const SystemConfigController = require('../controllers/systemConfigController')
const { validateSystemConfigSet, validateSystemConfigSetBatch, validatePagination } = require('../middleware/validation')
const { authenticateToken, requireAdmin } = require('../middleware/auth')

const router = express.Router()

// 获取系统配置
router.get('/:key',
  authenticateToken,
  requireAdmin,
  SystemConfigController.getConfig
)

// 设置系统配置
router.put('/:key',
  authenticateToken,
  requireAdmin,
  validateSystemConfigSet,
  SystemConfigController.setConfig
)

// 删除系统配置
router.delete('/:key',
  authenticateToken,
  requireAdmin,
  SystemConfigController.removeConfig
)

// 获取所有系统配置
router.get('/',
  authenticateToken,
  requireAdmin,
  SystemConfigController.getAllConfigs
)

// 批量设置系统配置
router.post('/batch',
  authenticateToken,
  requireAdmin,
  validateSystemConfigSetBatch,
  SystemConfigController.setBatchConfigs
)

// 重置配置到默认值
router.post('/:key/reset',
  authenticateToken,
  requireAdmin,
  SystemConfigController.resetToDefault
)

// 批量重置配置到默认值
router.post('/reset/all',
  authenticateToken,
  requireAdmin,
  SystemConfigController.resetAllToDefault
)

// 导出系统配置
router.get('/export/configs',
  authenticateToken,
  requireAdmin,
  SystemConfigController.exportConfigs
)

// 导入系统配置
router.post('/import/configs',
  authenticateToken,
  requireAdmin,
  SystemConfigController.importConfigs
)

// 验证配置值
router.post('/:key/validate',
  authenticateToken,
  requireAdmin,
  SystemConfigController.validateConfig
)

// 获取配置历史记录
router.get('/:key/history',
  authenticateToken,
  requireAdmin,
  validatePagination,
  SystemConfigController.getConfigHistory
)

// 清除配置缓存
router.delete('/cache/:key?',
  authenticateToken,
  requireAdmin,
  SystemConfigController.clearCache
)

// 获取配置统计信息
router.get('/stats/overview',
  authenticateToken,
  requireAdmin,
  SystemConfigController.getConfigStats
)

module.exports = router